package edu.gatech.cs2340.OregonTrail;
import javax.imageio.ImageIO;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Image;
import java.awt.Insets;
import javax.swing.ImageIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
//import java.util.ArrayList;
import java.util.Map;

/**
 * StartPanel class represents the start screen of the application
 * @author Tyler Johnston
 * @version 1.0.1
 */
public class StartPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	
	OregonTrailApp parentFrame;
	Image backgroundImage;
	
	/**
	 * Primary constructor for StartPanel class
	 * @param parentFrame The parent frame of this panel, used by listener to signal a screen transition
	 * @throws IOException 
	 */
	public StartPanel(final OregonTrailApp parentFrame) throws IOException {
		this.parentFrame = parentFrame;
		backgroundImage = ImageIO.read(StartPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/title.png"));

		setPreferredSize(new Dimension(524, 600));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{100, 323, 100, 0};
		gridBagLayout.rowHeights = new int[]{400, 75, 50, 75, 50, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		
		JLabel newGameBTN = new JLabel("");
		newGameBTN.setIcon(new ImageIcon(StartPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/btn_new.png")));
		GridBagConstraints gbc_newGameBTN = new GridBagConstraints();
		gbc_newGameBTN.insets = new Insets(0, 0, 5, 5);
		gbc_newGameBTN.gridx = 1;
		gbc_newGameBTN.gridy = 1;
		add(newGameBTN, gbc_newGameBTN);
		newGameBTN.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				parentFrame.showConfigScreen();
			}
		});
		
		JLabel loadGameBTN = new JLabel("");
		loadGameBTN.setIcon(new ImageIcon(StartPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/btn_continue.png")));
		GridBagConstraints gbc_loadGameBTN = new GridBagConstraints();
		gbc_loadGameBTN.insets = new Insets(0, 0, 5, 5);
		gbc_loadGameBTN.gridx = 1;
		gbc_loadGameBTN.gridy = 3;
		add(loadGameBTN, gbc_loadGameBTN);
		loadGameBTN.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					
				    try {
						BufferedReader in = new BufferedReader(new FileReader("saved.txt"));
					
						JOptionPane.showMessageDialog(null, "Loaded Game");

					    /* need to implement the node for where you are*/
						/*
						 * Takes in
						 * 1. Party member names, status 
						 * 2. Inventory 
						 * 3. Player's breakc, money, name,sickc , status, 
						 * 4. Day
						 * 5. Milestraveled
						 */
						
						Game game = parentFrame.getGame();
//						ArrayList<PartyMember> tempPartyMem = new ArrayList<PartyMember>();
						PartyMember temp1 = new PartyMember("temp1");
						PartyMember temp2 = new PartyMember("temp2");
						PartyMember temp3 = new PartyMember("temp3");
						PartyMember temp4 = new PartyMember("temp4");
						Player temp = new Banker("hehe");
						Party newParty = new Party(temp, temp1, temp2, temp3, temp4);
						game.intitalizeLoad(newParty, ePace.medium, eRation.medium);

						
						for(int i=0; i<4;i++)
						{
							game.getParty().getParty().get(i).setName(in.readLine());
							String str = in.readLine();
							if(str.equals(eStatus.alive))
							{
								game.getParty().getParty().get(i).setStatus(eStatus.alive);
							}
							if(str.equals(eStatus.sick))
							{
								game.getParty().getParty().get(i).setStatus(eStatus.sick);

							}
							if(str.equals(eStatus.dead))
							{
								game.getParty().getParty().get(i).setStatus(eStatus.dead);
							}
						}
						Object[]itemList = game.getParty().getPlayer().getInventory().getStock().keySet().toArray();
						Map<Item, Integer> playerStock = game.getParty().getPlayer().getInventory().getStock();
						for(int i=0; i<itemList.length;i++)
						{
							playerStock.put((Item)itemList[i], Integer.parseInt(in.readLine()));

						}
						in.readLine();
				
						game.getParty().getPlayer().setBreakC(Double.parseDouble(in.readLine()));
				
						game.getParty().getPlayer().setMoney(Integer.parseInt(in.readLine()));

						game.getParty().getPlayer().setName(in.readLine());

						game.getParty().getPlayer().setSickC(Double.parseDouble(in.readLine()));
						String str = in.readLine();
						if((str.equals(eStatus.alive)))
						{
							game.getParty().getPlayer().setStatus(eStatus.alive);
						}
						if((str.equals(eStatus.dead)))
						{
							game.getParty().getPlayer().setStatus(eStatus.dead);
						}
						if((str.equals(eStatus.sick)))
						{
							game.getParty().getPlayer().setStatus(eStatus.sick);
						}

						game.setDay(Integer.parseInt(in.readLine()));
						game.getTrail().setMilesTraveled(Integer.parseInt(in.readLine()));
						
						for(int i =0; i<game.getTrail().getLocations().length;i++)
						{
							game.getTrail().getLocations()[i].setVisited(Boolean.parseBoolean(in.readLine()));

						}
						for(int i =0; i<game.getTrail().getRiver().length;i++)
						{
							game.getTrail().getRiver()[i].setVisited(Boolean.parseBoolean(in.readLine()));
						}
						
						game.getTrail().setLocationIndex(Integer.parseInt(in.readLine()));
						game.getTrail().setRiverIndex(Integer.parseInt(in.readLine()));
						game.updatePlayPanel(parentFrame.getPlayPanel());					

						parentFrame.showPlayPanel();
				    
				    } catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
			});
	}
	
	public void paintComponent(Graphics g) {
	    super.paintComponent(g);

	    // Draw the background image.
	    g.drawImage(backgroundImage, 0, 0, null);
	  }
}
